﻿using System.Data;

namespace smp.QueryDb.Structure
{
    public class Column
    {
        private readonly Table _table;

        internal Column(Table table, string columnName, DbType dataType)
        {
            _table = table;
            DataType = dataType;
            Name = columnName;
            Declaration = string.Format("{0}.[{1}]", table.Declaration, columnName);
            TableDeclaration = string.Format("{0}", table.Declaration);
            OutputDeclaration = columnName;
        }

        internal Column(Table table, string columnName, DbType dataType, int fieldLength)
        {
            _table = table;
            DataType = dataType;
            Name = columnName;
            Declaration = string.Format("{0}.[{1}]", table.Declaration, columnName);
            TableDeclaration = string.Format("{0}", table.Declaration);
            OutputDeclaration = columnName;
            if(dataType == DbType.String)
            {
                FieldLength = fieldLength;
            }
        }

        internal Column(Table table, string columnName, DbType dataType, bool isNullable)
        {
            _table = table;
            DataType = dataType;
            IsNullable = isNullable;
            Name = columnName;
            Declaration = string.Format("{0}.[{1}]", table.Declaration, columnName);
            TableDeclaration = string.Format("{0}", table.Declaration);
            OutputDeclaration = columnName;
        }

        internal Column(Table table, string columnName, DbType dataType, bool isNullable, int fieldLength)
        {
            _table = table;
            DataType = dataType;
            IsNullable = isNullable;
            Name = columnName;
            Declaration = string.Format("{0}.[{1}]", table.Declaration, columnName);
            TableDeclaration = string.Format("{0}", table.Declaration);
            OutputDeclaration = columnName;
            if (dataType == DbType.String)
            {
                FieldLength = fieldLength;
            }
        }

        internal string Name { get; private set; }

        internal DbType DataType { get; private set; }

        internal bool IsNullable { get; private set; }

        internal int? FieldLength { get; private set; }

        public Column On(string namedTable)
        {
            Declaration = string.Format("[{0}].[{1}]",
                                        namedTable,
                                        Name);
            TableDeclaration = string.Format("[{0}]", namedTable);
            return this;
        }

        internal string Declaration { get; private set; }

        internal string OutputDeclaration { get; private set; }

        internal string TableDeclaration { get; private set; }

        public Column As(string name)
        {
            Declaration = string.Format("{0} AS [{1}]", Declaration, name);
            OutputDeclaration = name;
            return this;
        }
    }
}
